//package cn.fl.login.controller.filter;
//
//import cmm.comm.facade.framework.api.ISysCtrlCfgApiService;
//import cmm.comm.facade.framework.condition.SysCtrlCfgQueryCondition;
//import cmm.user.facade.system.api.ISysResPowerApiService;
//import cmm.user.facade.system.constant.SysResPowerConstant;
//import cmm.user.facade.system.dto.SysUsrDTO;
//import cn.cmm.base.framework.constant.BaseConstant;
//import cn.cmm.base.framework.context.ApplicationContextAccessor;
//import cn.cmm.base.framework.exception.BaseException;
//import cn.cmm.base.framework.utils.BaseLogUtils;
//import cn.cmm.base.framework.utils.CollectionUtils;
//import cn.cmm.base.framework.utils.StringUtils;
//import cn.cmm.base.framework.utils.UrlUtils;
//import cn.cmm.cache.framework.constant.CacheConstant;
//import cn.cmm.cache.framework.service.ICacheService;
//import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
//import cn.fl.framework.base.constant.WebBaseConstant;
//import cn.fl.framework.base.context.CurrentThreadContext;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.session.Session;
//import org.apache.shiro.subject.Subject;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import javax.servlet.*;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.List;
//
//public class UrlPowerFilter implements Filter {
//    private static final Logger logger = LoggerFactory.getLogger(UrlPowerFilter.class);
//
//    /**
//     * 系统资源服务
//     */
//    private ISysCtrlCfgApiService sysCtrlCfgApiService = (ISysCtrlCfgApiService) ApplicationContextAccessor.getBean("sysCtrlCfgApiService");
//
//    /**
//     * 系统资源服务
//     */
//    private ISysResPowerApiService sysResPowerApiService = (ISysResPowerApiService) ApplicationContextAccessor.getBean("sysResPowerApiService");
//
//    /**
//     * 缓存信息
//     */
//    private ICacheService cacheService = (ICacheService) ApplicationContextAccessor.getBean("cacheServiceImpl");
//
//
//    @Override
//    public void destroy() {
//    }
//
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
//            throws IOException, ServletException {
//
//        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
//
//        String url = httpServletRequest.getRequestURI();
//        if (StringUtils.isNotEmpty(url)) {
//            url = this.subStr(url);
//        }
//        //获取开关
//        Boolean resCtrl = getResPowerCtrl();
//        //对request和response进行一些预处理
//        request.setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html;charset=UTF-8");
//
//        Subject subject = SecurityUtils.getSubject();
//        Session session = subject.getSession(true);
//        SysUsrDTO sysUsrDTO = (SysUsrDTO) session.getAttribute(CurrentThreadContext.CURRENT_USER_INFO);
//        /**
//         * 1、判断开关是否打开，没有打开直接放行
//         * 2、判断用户是否登录，没有登录直接放行
//         * 3、在当前缓存中获取所有url,获取为空直接放行
//         * 4、判断当前访问url 是否在权限配置中，不存在直接先放行
//         * 5、判断当前角色是否有权限，有放行
//         */
//        //开关打开
//        if (resCtrl) {
//            //判断用户是否已经登录
//            if (subject.isAuthenticated() && null != sysUsrDTO) {
//                //获取所有url
//                Object obj = cacheService.get(CurrentThreadContext.CURRENT_RES_POWER, CacheConstant.CACHE_OPERATION_LOCAL);
//                BaseLogUtils.info(logger, "urlPowerFilter", "URL:" + url);
//
//                if (null != obj) {
//                    String allUrls = (String) obj;
//                    //当前访问url 在权限配置中判断角色下是否有权限
//                    if (allUrls.contains(url)) {
//                        //判断角色下是否有权限
//                        List<String> roleUrls = CurrentThreadContext.getCurrentResPowerUrl();
//                        if (CollectionUtils.isNotEmpty(roleUrls)) {
//                            if (!roleUrls.contains(url)) {
//                                BaseLogUtils.info(logger, "urlPowerFilter", CurrentThreadContext.getCurrentUserId()+"您当前角色【"+CurrentThreadContext.getCurrentRoleId()+"】无权限访问该URL:" + url);
//                                String prefixUrl = UrlUtils.getRequestUrl(httpServletRequest);
//                                httpServletResponse.sendRedirect(prefixUrl + "/login/login/toPowerErrorPage.do");
//                                return;
//                            }
//                        }else{
//                            BaseLogUtils.info(logger, "urlPowerFilter", CurrentThreadContext.getCurrentUserId()+"您当前角色【"+CurrentThreadContext.getCurrentRoleId()+"】url访问权限为空");
//                            String prefixUrl = UrlUtils.getRequestUrl(httpServletRequest);
//                            httpServletResponse.sendRedirect(prefixUrl + "/login/login/toPowerErrorPage.do");
//                            return;
//                        }
//                    }
//                } else {
//                    BaseLogUtils.info(logger, "urlPowerFilter", "未配置访问权限信息直接放行:" + url);
//                }
//            }
//        }
//        chain.doFilter(request, response);  //让目标资源执行，放行
//    }
//
//    @Override
//    public void init(FilterConfig arg0) throws ServletException {
//    }
//
//    /**
//     * @MethodName getResPowerCtrl
//     * @Description: 获取菜单权限开关
//     * @Author: wangpengtao@lionbridgecapital.cn
//     * @CreateDate: 2019/5/7 15:03
//     * @Version: V1.0.0
//     * @Param
//     * @Return java.lang.Boolean
//     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
//     */
//    private Boolean getResPowerCtrl() throws BaseException {
//        Boolean rtn = true;
//        try {
//            SysCtrlCfgQueryCondition qc = new SysCtrlCfgQueryCondition();
//            qc.setSysCd(WebBaseConstant.SYS_SYS_CD);
//            qc.setOrgCd(WebBaseConstant.SYS_P_ORG_CD);
//            qc.setCtrlCode(SysResPowerConstant.SYS_RES_POWER_CTL);
//            String code = sysCtrlCfgApiService.searchSysCtrlCfgByCtrlCode(qc);
//            if (StringUtils.isNotEmpty(code) && BaseConstant.IS_YESNO_NO_STR.equals(code)) {
//                rtn = false;
//            }
//        } catch (BaseException e) {
//            BaseLogUtils.error(logger, "getResPowerCtrl", "获取开关异常，" + e.getMessage(), e);
//        } catch (Exception e) {
//            BaseLogUtils.error(logger, "getResPowerCtrl", "获取开关异常，" + e.getMessage(), e);
//        }
//        BaseLogUtils.info(logger, "getResPowerCtrl", "获取菜单权限开关:" + rtn);
//        return rtn;
//    }
//
//    /**
//     * @MethodName subStr
//     * @Description: 截取访问url
//     * @Author: wangpengtao@lionbridgecapital.cn
//     * @CreateDate: 2019/5/13 18:01
//     * @Version: V1.0.0
//     * @Param url
//     * @Return java.lang.String
//     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
//     */
//    private String subStr(String url) {
//        if (StringUtils.isNotEmpty(url)) {
//            String projectName = PropertiesCacheUtils.getProperty("web.web.properties.project");
//            if (StringUtils.isNotEmpty(projectName)) {
//                url = url.substring(projectName.length());
//            }
//            int i = url.indexOf("?");
//            if (i > 0) {
//                url = url.substring(0, i);
//            }
//        }
//        return url;
//    }
//}
